var express = require("express");
var bodyParser = require('body-parser');

var senecaEmailer = require("seneca")().client({
    host: "192.168.0.2",
    port: 8080
});
var senecaProductManager = require("seneca")().client({
    host: "192.168.0.3",
    port: 8080
});
var senecaOrderProcessor = require("seneca")().client({
    host: "192.168.0.4",
    port: 8080
});

function api(options) {
    var seneca = this;

    /**
     * Gets the full list of products.
     */
    seneca.add({area: "ui", action: "products"}, function(args, done) {
        senecaProductManager.act({area: "product", action: "fetch"}, function(err, result) {
            done(err, result);
        });
    });

    /**
     * Get a product by id.
     */
    seneca.add({area: "ui", action: "productbyid"}, function(args, done) {
        senecaProductManager.act({area: "product", action: "fetch", criteria: "byId", id: args.id}, function(err, result) {
            done(err, result);
        });
    });

    /**
     * Creates an order to buy a single prodct.
     */
    seneca.add({area: "ui", action: "createorder"}, function(args, done) {
        senecaProductManager.act({area: "product", action: "fetch", criteria: "byId", id: args.id}, function(err, product) {
            if(err) done(err, null);
            senecaOrderProcessor.act({ area: "orders", action: "create", products: [product], email: args.email, name: args.name }, function(err, order) {
                done(err, order);
            });
        });
    });

    this.add("init:api", function(msg, respond){

    });
}
module.exports = api;
var seneca = require("seneca")();
seneca.use(api);

const SenecaWeb = require('seneca-web')
const Express = require('express')
var express = Express();

express.use(require("body-parser").json());
seneca.use(SenecaWeb, {
    context: express,
    adapter: require('seneca-web-adapter-express'),
    // defaults --
    options: {
        parseBody: true,
        includeRequest: true,
        includeResponse: true
    }
})



seneca.ready(function (err) {
    seneca.act('role:web',{ routes: [{
        prefix: '/api',
        pin:    'area:ui,action:*',
        map: {
          products:    {GET:true},
          productbyid: {GET:true, suffix:'/:id'},
          createorder: {POST:true}
        }
    }]})
    const app = seneca.export('web/context')()
    app.listen(3000)
});
